{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://plot.ly/python/t-test/\n",
    "\n",
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "import plotly.figure_factory as ff\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.dtype size changed\")\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.ufunc size changed\")\n",
    "\n",
    "py.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample 画出散点图\n",
    "xs_nd = np.linspace(start=-4, stop=4, num=200)\n",
    "\n",
    "norm_trace_0 = go.Scatter(\n",
    "    x = xs_nd,\n",
    "    y = scipy.stats.norm.pdf(x=xs_nd, loc=0, scale=1),\n",
    "    name = 'Mean is 0',\n",
    "    mode = 'lines '\n",
    ")\n",
    "\n",
    "norm_trace_1 = go.Scatter(\n",
    "    x = xs_nd,\n",
    "    y = scipy.stats.norm.pdf(x=xs_nd, loc=2, scale=1),\n",
    "    name = 'Mean is 2',\n",
    "    mode = 'lines+markers'\n",
    ")\n",
    "\n",
    "norm_fig = go.Figure(data=[norm_trace_0, norm_trace_1])\n",
    "\n",
    "py.offline.iplot(norm_fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成两组正态数据\n",
    "data_nd_1 = np.random.normal(loc=0, scale=1, size=1000)\n",
    "data_nd_2 = np.random.normal(loc=2, scale=1, size=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample画出频率图\n",
    "\n",
    "hist_trace_1 = go.Histogram(\n",
    "    x = data_nd_1,\n",
    "    autobinx = True,\n",
    "    name = 'Mean of 0',\n",
    ")\n",
    "\n",
    "hist_trace_2 = go.Histogram(\n",
    "    x = data_nd_2,\n",
    "    xbins = dict(\n",
    "        start = np.min(data_nd_1),\n",
    "        size = 0.1,\n",
    "        end = np.max(data_nd_2),\n",
    "    ),\n",
    "    name = 'Mean of 2',\n",
    ")\n",
    "\n",
    "# 如果hist_trace_2设置了xbins, hist_trace_1的autobinx将会失效\n",
    "hist_fig = go.Figure(data=[hist_trace_1, hist_trace_2])\n",
    "py.offline.iplot(hist_fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample画出分布图, 猜测里面封装了对频率图的操作(且histnorm = 'probability')\n",
    "dist_trace_0 = ff.create_distplot(\n",
    "    hist_data = [data_nd_1, data_nd_2],\n",
    "    group_labels = ['Mean of 0', 'Mean of 2'],\n",
    "    bin_size = 0.1,\n",
    "    curve_type = 'kde', # normal 比 kde 更平滑\n",
    "    histnorm = 'probability',\n",
    "    colors = ['#0000FF', '#00FF00'],\n",
    "    show_hist = False, # 控制是否显示柱子\n",
    ")\n",
    "py.offline.iplot(dist_trace_0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1个Sample的T-Test\n",
    "t_1sample_result = scipy.stats.ttest_1samp(a=data_nd_1, popmean=0.0)\n",
    "t_1sample_result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2个Sample的T-Test, 等方差 (ind: indenpent)\n",
    "t_ind_result = scipy.stats.ttest_ind(a=data_nd_1, b=data_nd_2, equal_var=True)\n",
    "t_ind_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# loc接近0的时候, 概率判断出: 数据不足以否定0假设\n",
    "data_nd_3 = np.random.normal(loc=0.1, scale=1, size=30)\n",
    "t_ind_result = scipy.stats.ttest_ind(a=data_nd_1, b=data_nd_3, equal_var=True)\n",
    "t_ind_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_nd_4 = np.random.normal(loc=0, scale=1.1, size=30)\n",
    "t_ind_result = scipy.stats.ttest_ind(a=data_nd_1, b=data_nd_4, equal_var=False)\n",
    "t_ind_result "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
